package com.fruit.controller.wap.invite;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.sms.SmsCodeValidate;
import com.fruit.pojo.user.UserInfo;
import com.fruit.pojo.user.UserLogin;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.ValidateUtil;

@Controller
@RequestMapping(value = "/wap/")
public class InviteController extends BaseController {

	@Autowired
	private UserManager userService;
	
	@Autowired
	private SmsCodeManager smsCodeService;
	 
	// 邀请注册
	@RequestMapping(value = "invite", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> register(UserLogin ul,HttpSession session,HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "邀请注册");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String userId=request.getParameter("userId");
		String type=request.getParameter("type");
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if(StringUtils.isEmpty(ul.getPwd())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			if(StringUtils.isEmpty(userId)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "缺少参数！"));
				return resMap;
			}
			ul.setUserId(Integer.parseInt(userId));
			resMap = userService.saveInviteUser(ul,type);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	/**
	 * 发送验证码
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/send", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> send(UserLogin ul, String type,HttpServletResponse response, HttpServletRequest request) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if(ri != null){
				 resMap.put("result", ri);
				 return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
				return resMap;
			}
			UserLogin login = userService.getUserLoginByPhone(ul);
			if(StringUtils.equals(type, "login")||StringUtils.equals(type, "password")){
				if(login == null){
					resMap.put("result", this.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
					return resMap;
				}
			}
			if(StringUtils.equals(type, "register")||StringUtils.equals(type, "invite")){
				if(login != null){
					resMap.put("result", this.initResult(ResultInfo.ERROR, "is_exists", "该手机号码已注册!"));
					return resMap;
				}
			}
			SmsCodeValidate sms = new SmsCodeValidate();
			sms.setPhone(ul.getPhone());
			sms.setTypes(type);
			resMap = smsCodeService.sendSmsCode(sms);
			
		}catch(Exception e){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "发送失败"));
		}
		return resMap;
	}
	
	@RequestMapping(value = "getScanCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getScanCode(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "解析二维码");
		String code = request.getParameter("code");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (code != null && !"".equals(code)) {
				String userId = code.substring(4, code.length() - 3);
				UserInfo userInfo = userService.getUser(Integer.parseInt(userId));
				if(userInfo != null){
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
					resMap.put("userInfo",userInfo);
				}else{
					resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "未找到用户!"));
				}
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "参数错误!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
}
